<?php
class registration implements gatekeeper
{
	function __construct()
	{
		/*
		*	Registration Request Variables
		*
		*	- Username(POST)
		*	- Password(POST)
		*	- Beta (POST or GET)
		*
		*	Retrieve GET and POST Data
		*	Global needed Classes
		*/
		Global $beta, $users, $lib, $auth, $mySql;
		
		$this->setField("beta");
		$this->setField("username");
		$this->setField("password");
		$this->setField("email");
		
		$this->lib = $lib;
		$this->mySql = $mySql;
		$this->auth = $auth;
	}
	public function runClass()
	{	
		session_start();
		// Checks for a Direct Hit Link
		if(!EMPTY($this->beta) && EMPTY($this->username)) // CHECK FOR A DIRECT BETA CODE LINK
		{
			if(db_beta::checkBetaCode($this->beta)) // Checks for Beta Code 
			{
				$this->dspRegForm();
			}
		}
		
		// Checks if the Form has been Passed
		elseif(!EMPTY($this->username)) // CHECK FOR THE POST
		{
			if(db_beta::checkBetaCode($this->beta))
			{
				if(!db_users::chkUsernameExists($this->username))  // Checks to make sure there are no duplicate names
				{	
					if($this->lib->checkEmail($this->email))
					{
						db_beta::useBetaCode($this->beta);
						$data[username] = $this->username;
						$data[password] = $this->password;
						$data[email] = $this->email;
						db_users::addUser($data);
						$this->lib->redirect("?");
					}
					else
					{
						$this->dspRegForm("The Email you have entered is invalid");							
					}					
				}
				else
				{
				
					$this->dspRegForm("The Username you have picked has already been taken.");
					
				}
			}
			else
			{
				$this->dspRegForm("The Beta Code you have entered is not in use");
			}
		}
		else
		{
			$this->dspRegForm();
		}
	}	
	private function dspRegForm($msg="")
	{
		echo "<div id=firstbox>".$msg."</div>";
		?>
			<div id=secondbox>
			<span class=header>Registration</span>
				<form method=POST>
						<input type=hidden name=reg[confirm] value=true>
						<div style="width: 200px; padding: 5px;">Name : <span class=inputbox><input type=text size=30 value='<?php echo $this->username ?>' name="reg[username]"></span></div>
						<div style="width: 200px; padding: 5px;">Password : <span class=inputbox><input type=password size=30 name="reg[password]"></span></div>
						<div style="width: 200px; padding: 5px;">Email : <span class=inputbox><input type=text size=30 value='<?php echo $this->email ?>' name="reg[email]"></span></div>
						<div style="width: 200px; padding: 5px;">Beta Code : <span class=inputbox><input type=text size=30 value='<?php echo $this->beta ?>' name="reg[beta]"></span></div>
						<span class=inputbox><input type=submit value="Go!"></span>
					</form>
			</div>
		<?php
	}
	/** 
	* Set ID fields in order of inbound presedence 
	* 
	* @param string $field 
	*/ 
	private function setField($field, $value = FALSE) 
	{ 
		// Manuall Set Value 
		if (FALSE != $value) 
		{ 
			$this->$field = $value; 
		} 
		// Form Data First 
		elseif (FALSE != $_POST["reg"][$field]) 
		{ 
			$this->$field = $_POST["reg"][$field]; 
		} 
		// General Post data next 
		elseif (FALSE != $_POST[$field]) 
		{ 
			$this->$field = $_POST[$field]; 
		} 
		// Get Data last 
		elseif (FALSE != $_GET[$field]) 
		{ 
			$this->$field = $_GET[$field]; 
		} 
		// But it can't be an array 
		if (FALSE != is_array($this->$field)) 
		{ 
			unset($this->$field); 
		} 
	}
}
	
?>